#ifndef __demo_gpio_kenny_kfjiweiiur89u83r33iwuee3e__
#define __demo_gpio_kenny_kfjiweiiur89u83r33iwuee3e__

#include "plugs/virt-gpio/client-gpio.hpp"

#include "test/test.hpp"

namespace test
{
    using namespace ha;

    //--------------------------------------------------------------------------------
    struct demo_gpio_t : public hude::test::case_t
    {
        enum : uint16_t { emCOUNT = 2U };

        using gpio_t = ha::gpio_t< ha::gpio_type_t::emGPIO_EMPTY >;
        // using gpio_t = ha::brick::gpio_client_t;

        hude::aio::aio_t<> aio_;
        bool               ret_;

        // 测试过程
        bool run() override;

        bool start();
        void stop();
    };

    //--------------------------------------------------------------------------------
    inline bool demo_gpio_t::run()
    {
        aio_ = hude::aio::context_t<>::aio();

        auto ret = aio_->run( [ = ]() {
            std::thread{ [=] { ret_ = start(); stop(); } }.detach();
        } );

        return ret == 0 && ret_;
    }

    //--------------------------------------------------------------------------------
    inline bool demo_gpio_t::start()
    {
        auto test = []( gpio_t& gpio, uint16_t index )
        {
            log_dug( "time: 0 ========================================" );
            if( *( gpio.enable( index ) ) == true )
            {
                gpio.enable( index, false );
            }

            log_dug( "time: 1 ========================================" );
            if( *( gpio.enable( index ) ) == true ) return false;
            if( ! gpio.enable( index, true ) ) return false;
            if( *( gpio.enable( index ) ) == false ) return false;

            log_dug( "time: 2 ========================================" );
            if( *( gpio.direction( index ) ) == gpio_t::dir_t::emDOUT ) return false;
            if( ! gpio.direction( index, gpio_t::dir_t::emDOUT ) ) return false;
            if( *( gpio.direction( index ) ) == gpio_t::dir_t::emDIN ) return false;

            log_dug( "time: 3 ========================================" );
            if( *( gpio.value( index ) ) == true ) return false;
            if( ! gpio.value( index, true ) ) return false;
            if( *( gpio.value( index ) ) == false ) return false;

            log_dug( "time: 4 ========================================" );
            if( *( gpio.enable( index ) ) == false ) return false;
            if( ! gpio.enable( index, false ) ) return false;
            if( *( gpio.enable( index ) ) == true ) return false;

            log_dug( "time: 5 ========================================" );

            return true;
        };

        gpio_t gpio;

        for( uint16_t index = 0; index < emCOUNT; ++index )
        {
            if( ! test( gpio, index ) ) { log_err( "test %u", index ); return false; }
            if( ! test( gpio, index ) ) { log_err( "test %u", index ); return false; }
            if( ! test( gpio, index ) ) { log_err( "test %u", index ); return false; }

            log_inf( "\t case gpio( %u / %u ) ok", index, emCOUNT );
        }

        return true;
    }

    //--------------------------------------------------------------------------------
    inline void demo_gpio_t::stop()
    {
        this->aio_->cancel();
    }

    TEST_CASE_ADD( "gpio", demo_gpio_t, false );

} // namespace test

#endif // __demo_gpio_kenny_kfjiweiiur89u83r33iwuee3e__



